/**
 * 
 */
package com.apache.portal.common.connector.impl;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.apache.api.vo.ParamsVo;
import com.apache.portal.common.annotion.AnntationBean;
import com.apache.portal.common.connector.BeforlerInterceptorAdapter;
import com.apache.portal.common.util.PortalPubFactory;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.ToolsUtil;

/**
 * @author guogf
 * 发送验证码
 */
@AnntationBean(name = "createInfo")
public class SetCreateInfoAdapterImpl extends BeforlerInterceptorAdapter {

	/* (non-Javadoc)
	 * @see com.iussoft.portal.common.connector.InterceptorAdapter#beforPreHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, com.apache.api.vo.ParamsVo)
	 */
	public boolean beforPreHandle(HttpServletRequest request, HttpServletResponse response, ParamsVo param) {
		Map<String, Object> resultMap = param.getParams();
		String modelTypes = String.valueOf(resultMap.get("modelTypes"));
		LoginUser loginUser = PortalPubFactory.getInstance().getLoginUser(request);
		String str[] = modelTypes.split(",");
		for (int i = 0; i < str.length; i++) {
			if (str[i].startsWith("i_") || str[i].startsWith("u_") || str[i].startsWith("ib_")
					|| str[i].startsWith("ub_")) {
				String prefix = str[i].substring(2);
				if (str[i].startsWith("ib_") || str[i].startsWith("ub_")) {
					prefix = str[i].substring(3);
				}
				if (str[i].startsWith("i_") || str[i].startsWith("ib_")) {
					if (ToolsUtil.isNull(String.valueOf(resultMap.get(prefix + ".createTime"))))
						resultMap.put(prefix + ".createTime", DateUtils.Now.fmt_yyyyMMdd_HHmmss());
					if (ToolsUtil.isNull(String.valueOf(resultMap.get(prefix + ".delStatus"))))
						if ("uctUser".equals(prefix)) {
							resultMap.put(prefix + ".delStatus", "0");
						} else {
							resultMap.put(prefix + ".delStatus", "F");
						}
					if (ToolsUtil.isNull(String.valueOf(resultMap.get(prefix + ".createUser")))) {
						if (null != loginUser) {
							resultMap.put(prefix + ".createUser", loginUser.getUserEname());
							if (StrUtil.isNull(String.valueOf(resultMap.get(prefix + ".orgId")))) {
								resultMap.put(prefix + ".orgId", loginUser.getOrgId());
							}
							if (StrUtil.isNull(String.valueOf(resultMap.get(prefix + ".deptId")))) {
								resultMap.put(prefix + ".deptId", loginUser.getDeptId());
							}
						}
					}
				} else {
					if (ToolsUtil.isNull(String.valueOf(resultMap.get(prefix + ".modifyUser")))) {
						if (null != loginUser)
							resultMap.put(prefix + ".modifyUser", loginUser.getUserEname());
					}
					if (ToolsUtil.isNull(String.valueOf(resultMap.get(prefix + ".modifyTime"))))
						resultMap.put(prefix + ".modifyTime", DateUtils.Now.fmt_yyyyMMdd_HHmmss());
				}
			}
		}
		resultMap.put("logReqIp", getUserIp(request));
		if (null != loginUser)
			resultMap.put("logOperatorUser", loginUser.getUserEname());
		param.setParams(resultMap);
		return true;
	}

	/**
	 * description:  获取访问IP
	 * @param request 当前请求
	 * @return   
	 * @author Hou Dayu  
	 */
	private String getUserIp(HttpServletRequest request) {
		String ip = "";
		try {
			ip = request.getHeader("x-forwarded-for");
			if (ToolsUtil.isNull(ip) || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("Proxy-Client-IP");
			}
			if (ToolsUtil.isNull(ip) || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("WL-Proxy-Client-IP");
			}
			if (ToolsUtil.isNull(ip) || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getRemoteAddr();
			}
			if (ToolsUtil.isNotNull(ip) && ip.length() > 32) {
				ip = ip.substring(0, 32);
			}
		} catch (Exception e) {
		}
		return ip;
	}
}
